package cn.webrx.bs.service;

import cn.webrx.bs.entity.Ad;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-13 17:44
 */
public class AdDaoImpl implements AdDao {
    @Override
    public int add(Ad ad) {
        int num = 0;
        try {
            num = run.update("insert into bs_ad values(null,?,?,?,?,?,?)",ad.getTitle(),ad.getUrl(),ad.getImg(),ad.isShows(),ad.getCdt(),ad.getSort());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public int add(String title, String url, String img) {
        int num = 0;
        try {
            num = run.update("insert into bs_ad(title,url,img,cdt) values(?,?,?,?)",title,url,img,new Date());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public int deleteById(int id) {
        int num = 0;
        try {
            num = run.update("delete from bs_ad where id = ?",id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public int deleteById(int id,String url) {
        int num = 0;
        //删除数据记录
        num = deleteById(id);

        //实现删除图片
        File file = new File(url);
        if(file.exists()) file.delete();
        return num;
    }

    @Override
    public int delete(Ad ad) {
        return deleteById(ad.getId());
    }

    @Override
    public int update(Ad ad) {
        int num = 0;
        try{
            num = run.update("update bs_ad set title=?,url=?,sort=?,shows=? where id = ?",ad.getTitle(),ad.getUrl(),ad.getSort(),ad.isShows(),ad.getId());
        }catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int update(int id,boolean shows) {
        int num = 0;
        try{
            num = run.update("update bs_ad set shows = ? where id = ?",shows,id);
        }catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public Ad queryById(int id) {
        Ad ad = null;
        try{
            ad = run.query("select * from bs_ad where id=?",new BeanHandler<Ad>(Ad.class),id);
        }catch(Exception e){
            e.printStackTrace();
        }
        return ad;
    }

    @Override
    public List<Ad> queryAll() {
        List<Ad> list = null;
        try {
            list = run.query("select * from bs_ad order by sort desc", new BeanListHandler<>(Ad.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Ad> show() {
        List<Ad> list = null;
        try {
            list = run.query("select * from bs_ad where shows order by sort desc", new BeanListHandler<>(Ad.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
